<?php

/**
 * Формын алдааны мэдээллийг харуулах
 * 
 * @param mixed $formRow
 */
function formError($formRow) {
    $rows = is_array($formRow) ? $formRow : array($formRow);
    $errors = array();

    foreach ($rows as $row) {
        if ($row->hasError()) {
            $errors[] = $row->getError();
        }
    }

    if (count($errors)) {
        return '<img src="/images/icons/error.png" align="absmiddle" title="' . join(" - ", $errors) . '" alt="' . join(" - ", $errors) . '" />';
    }

    return '';
}

/**
 * Формын тусламжийн мэдээллийг харуулах
 * 
 * @param mixed $formRow
 */
function formHelp($formRow) {
    $rows = is_array($formRow) ? $formRow : array($formRow);
    $helps = array();

    foreach ($rows as $row) {
        $helps[] = $row->getParent()->getWidget()->getHelp($row->getName());
    }

    if (count($helps)) {
        return '<img src="/images/icons/help.png" align="absmiddle" title="' . join(" - ", $helps) . '" alt="' . join(" - ", $helps) . '" />';
    }

    return '';
}

function numberFormat($number) {
    return number_format($number, 2, '.', ',');
}

function dateFormat($str) {
    $date = date_create($str);
    return date_format($date, 'Y-m-d');
}

function pager_navigation($pager, $uri) {
    $navigation = '&nbsp;';

    if ($pager->haveToPaginate()) {
        $uri .= ( preg_match('/\?/', $uri) ? '&' : '?') . 'page=';

        // First and previous page
        if ($pager->getPage() != 1) {
            $navigation .= link_to(image_tag('icons/first.png', 'align=absmiddle'), $uri . '1');
            $navigation .= link_to(image_tag('icons/previous.png', 'align=absmiddle'), $uri . $pager->getPreviousPage()) . ' ';
        }

        // Pages one by one
        $links = array();
        foreach ($pager->getLinks() as $page) {
            $links[] = link_to_unless($page == $pager->getPage(), $page, $uri . $page);
        }
        $navigation .= join('  ', $links);

        // Next and last page
        if ($pager->getPage() != $pager->getLastPage()) {
            $navigation .= ' ' . link_to(image_tag('icons/next.png', 'align=absmiddle'), $uri . $pager->getNextPage());
            $navigation .= link_to(image_tag('icons/last.png', 'align=absmiddle'), $uri . $pager->getLastPage());
        }
    }

    return $navigation;
}
